package com.example.oracle.service.impl;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.oracle.config.MysqlDataSourceConfig;
import com.example.oracle.config.OracleDataSourceConfig;
import com.example.oracle.dao.ISmsToMysqlMapper;
import com.example.oracle.dao.ISmsToOracleMapper;
import com.example.oracle.entity.SmsTo;
import com.example.oracle.service.ISmsToService;

@Service
public class SmsToServiceImpl implements ISmsToService{

	@Autowired
	private ISmsToOracleMapper smsToOracleMapper;
	
	@Autowired
	private ISmsToMysqlMapper smsToMysqlMapper;

	@Override
	public List<SmsTo> getPage(boolean oralce,SmsTo smsTo) {
		if (oralce) {
			return this.smsToOracleMapper.getPage(smsTo);
		}
		return this.smsToMysqlMapper.getPage(smsTo);
	}

	@Override
	@Transactional(value=OracleDataSourceConfig.transactionManagerName)
	public void batchAddOracle(List<SmsTo> smsTos) {
		if (smsTos == null || smsTos.isEmpty()) {
			return ;
		}
		final int maxBatchRow = 1000;
		final int batchCount = smsTos.size()%maxBatchRow == 0 ? smsTos.size()/maxBatchRow : (smsTos.size()/maxBatchRow+1);
		int row;
		for(int i=0;i<batchCount;i++) {
			if (i+1 == batchCount) {
				row = smsTos.size()%maxBatchRow==0?maxBatchRow:smsTos.size()%maxBatchRow;
			}else {
				row = maxBatchRow;
			}
			this.smsToOracleMapper.batchAdd(smsTos.subList(i * maxBatchRow, i * maxBatchRow + row));
		}
	}

	@Override
	@Transactional(value=MysqlDataSourceConfig.transactionManagerName)
	public void batchAddMysql(List<SmsTo> smsTos) {
		if (smsTos == null || smsTos.isEmpty()) {
			return ;
		}
		final int maxBatchRow = 10000;
		final int batchCount = smsTos.size()%maxBatchRow == 0 ? smsTos.size()/maxBatchRow : (smsTos.size()/maxBatchRow+1);
		int row;
		for(int i=0;i<batchCount;i++) {
			if (i+1 == batchCount) {
				row = smsTos.size()%maxBatchRow==0?maxBatchRow:smsTos.size()%maxBatchRow;
			}else {
				row = maxBatchRow;
			}
			this.smsToMysqlMapper.batchAdd(smsTos.subList(i * maxBatchRow, i * maxBatchRow + row));
		}
	}

}
